LAB 2: Image Arithmetic Operations

Image Arithmetic for Grayscale Images

Instructions: We provide code snippets for performing image arithmetic you learned from the lectures. These include: brighter, darker, higher contrast, lower contrast, and negative images. You will need to import the libraries: OpenCV, matplotlib, and NumPy. For submission, please remember to name your working directory 6088xxx_lab2, save your output image and name it with the lab and exercise number. Your every result should follow the layout shown in an example output; properly adjust figsize and the title fontsize to fit your selected images.

Pick a color image you like. Instead of loading an image in grayscale as we have done earlier, we will try reading it as is (without a flag). Expectedly, we obtain a BGR image. We then convert it to grayscale using OpenCV cvtColor function. Look familiar? This is the same function that we use to convert from BGR to RGB. The only difference is the transform specification: cv2.COLOR_BGR2GRAY. We display side-by-side the color and gray images.

In [2]:
imC = cv2.imread('tree.jpg')
im = cv2.cvtColor(imC, cv2.COLOR_BGR2GRAY)
Exercise #1: In the following lines of code, we show you how to perform addition on a grayscale image, making it uniformly brighter. Refer to the lecture on how to do subtraction, multiplication, division, and image negative. Below, we show you our complete set of output, you do yours on your selected image. You will need to vary the weight (increase or decrease it), observe the results, and select the weight that best fitted your work.
In [4]:
weight = 100                              #Specify a constant to add/subtract to an image
brighter = cv2.add(im,weight)             #Image addition, an image is uniformly brighter

Image Arithmetic for Color Images

Next, we apply the same five operations: brighter/darker, higher/lower contrast, and negative to a color image. Unlike grayscale, we now have three channels that we need to handle. The arithmetic is essentially the same as before but we repeat it three times for three channels. As an example, we pick another color image to work on and detail how to brighten it in two steps. The code and the result is shown below.

  1. Use NumPy to create a placeholder for a new (brighter) image. This is a 3D array the same size and shape as the original image. Its data type is an 8-bit unsigned integer. All elements, i.e. pixel values, are set to 1.
  2. Use for-loop to process the three BGR channels. Each iteration of the loop adds weight to the pixel values of each channel of an input image and store it into the respective channel of an output image.
In [7]:
weight = 100
brighter = np.ones(im.shape,dtype='uint8')
for i in np.arange(0,3):
    brighter[:,:,i] = cv2.add(im[:,:,i], weight)
Exercise #2: follow the brighter example to perform darker, higher- and lower-contrast. Then, do a negative image. The code for negative operation is exactly the same as that use in grayscale, i.e. there is no need to explicitly perform arithmetic on each channel separately. Adjust the weights to fit your image so you can clearly see effects of all five operators. Display your results in a 2x3 grid as shown in an example below.
READY FOR SUBMISSION? Check your working directory, 6088xxx_lab2, it should now contained the following files. Please zip the folder and submit it to MyCourse.